import { Component, Vue, Inject } from 'vue-property-decorator';

import { required } from 'vuelidate/lib/validators';

import AlertService from '@/shared/alert/alert.service';

import TeacherService from '@/entities/teacher/teacher.service';
import { ITeacher } from '@/shared/model/teacher.model';

import StudentService from '@/entities/student/student.service';
import { IStudent } from '@/shared/model/student.model';

import { IUserRole, UserRole } from '@/shared/model/user-role.model';
import UserRoleService from './user-role.service';

const validations: any = {
  userRole: {
    roleName: {
      required,
    },
  },
};

@Component({
  validations,
})
export default class UserRoleUpdate extends Vue {
  @Inject('userRoleService') private userRoleService: () => UserRoleService;
  @Inject('alertService') private alertService: () => AlertService;

  public userRole: IUserRole = new UserRole();

  @Inject('teacherService') private teacherService: () => TeacherService;

  public teachers: ITeacher[] = [];

  @Inject('studentService') private studentService: () => StudentService;

  public students: IStudent[] = [];
  public isSaving = false;
  public currentLanguage = '';

  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (to.params.userRoleId) {
        vm.retrieveUserRole(to.params.userRoleId);
      }
      vm.initRelationships();
    });
  }

  created(): void {
    this.currentLanguage = this.$store.getters.currentLanguage;
    this.$store.watch(
      () => this.$store.getters.currentLanguage,
      () => {
        this.currentLanguage = this.$store.getters.currentLanguage;
      }
    );
    this.userRole.teachers = [];
    this.userRole.students = [];
  }

  public save(): void {
    this.isSaving = true;
    if (this.userRole.id) {
      this.userRoleService()
        .update(this.userRole)
        .then(param => {
          this.isSaving = false;
          this.$router.go(-1);
          const message = 'A UserRole is updated with identifier ' + param.id;
          return this.$root.$bvToast.toast(message.toString(), {
            toaster: 'b-toaster-top-center',
            title: 'Info',
            variant: 'info',
            solid: true,
            autoHideDelay: 5000,
          });
        })
        .catch(error => {
          this.isSaving = false;
          this.alertService().showHttpError(this, error.response);
        });
    } else {
      this.userRoleService()
        .create(this.userRole)
        .then(param => {
          this.isSaving = false;
          this.$router.go(-1);
          const message = 'A UserRole is created with identifier ' + param.id;
          this.$root.$bvToast.toast(message.toString(), {
            toaster: 'b-toaster-top-center',
            title: 'Success',
            variant: 'success',
            solid: true,
            autoHideDelay: 5000,
          });
        })
        .catch(error => {
          this.isSaving = false;
          this.alertService().showHttpError(this, error.response);
        });
    }
  }

  public retrieveUserRole(userRoleId): void {
    this.userRoleService()
      .find(userRoleId)
      .then(res => {
        this.userRole = res;
      })
      .catch(error => {
        this.alertService().showHttpError(this, error.response);
      });
  }

  public previousState(): void {
    this.$router.go(-1);
  }

  public initRelationships(): void {
    this.teacherService()
      .retrieve()
      .then(res => {
        this.teachers = res.data;
      });
    this.studentService()
      .retrieve()
      .then(res => {
        this.students = res.data;
      });
  }

  public getSelected(selectedVals, option): any {
    if (selectedVals) {
      for (let i = 0; i < selectedVals.length; i++) {
        if (option.id === selectedVals[i].id) {
          return selectedVals[i];
        }
      }
    }
    return option;
  }
}
